/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QtDebug>
#include <QCoreApplication>
#include <QPainter>

#include "timerlistmodel.h"
#include "qtvomp.h"


TimerListModel::TimerListModel( MainWindow * mainWindow, QObject *parent ) : QAbstractListModel( parent ),
    m_mainWindow( mainWindow )
{
}


int TimerListModel::rowCount( const QModelIndex &/*parent*/ ) const
{
    return m_mainWindow->timerList()->count();
}


QVariant TimerListModel::data( const QModelIndex &index, int role ) const
{
    if ( !index.isValid() )
        return QVariant();

    if ( index.row() >= m_mainWindow->timerList()->size() )
        return QVariant();

    switch ( role )
    {
        case Qt::DisplayRole:
        case TimerListModel::TimerFileRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).file;
        }

        case TimerListModel::TimerActiveRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).active;
        }

        case TimerListModel::TimerRecordingRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).recording;
        }

        case TimerListModel::TimerPendingRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).pending;
        }

        case TimerListModel::TimerChannelNumberRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).channelNumber;
        }

        case TimerListModel::TimerStartTimeRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).startTime;
        }

        case TimerListModel::TimerStopTimeRole:
        {
            return m_mainWindow->timerList()->at( index.row() ).stopTime;
        }

/*        case TimerListModel::TimerNumberRole:
        {
            return m_mainWindow->channelList()->at( index.row() ).channel;
        }
*/
        default:
        {
            return QVariant();
        }
    }
}


bool TimerListModel::insertRows( int position, int rows, const QModelIndex &/*parent*/ )
{
    beginInsertRows( QModelIndex(), position, position + rows - 1 );

    for ( int row = 0; row < rows; ++row )
    {
        TimerEntry entry;
        m_mainWindow->timerList()->insert( position, entry );
    }

    endInsertRows();

    return true;
}


bool TimerListModel::removeRows( int position, int rows, const QModelIndex &/*parent*/ )
{
    if ( rows == 0 )
    {
        return false;
    }

    beginRemoveRows( QModelIndex(), position, position + rows - 1 );

    for ( int row = 0; row < rows; ++row )
    {
        m_mainWindow->timerList()->removeAt( position );
    }

    endRemoveRows();

    return true;
}


void TimerListModel::setTimerData( TimerEntry entry, int row )
{
    if ( row > m_mainWindow->timerList()->count() - 1 || row < 0 )
    {
        qWarning() << "TimerListModel::setTimerName: row is out of range";
        return;
    }
    ( *m_mainWindow->timerList() )[ row ] = entry;
}
